package org.example.service.impl;

import org.example.domain.Menu;
import org.example.domain.RoleMenu;
import org.example.mapper.MenuMapper;
import org.example.mapper.RoleMenuMapper;
import org.example.service.MenuService;
import org.example.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* @author 14168
* @description 针对表【sys_menu】的数据库操作Service实现
* @createDate 2024-05-22 19:07:25
*/
@Service
public class MenuServiceImpl implements MenuService{

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Override
    public List<MenuVo> list(Integer userId) {
        List<MenuVo> menuList = menuMapper.list(userId);
        return menuList;
    }

    @Override
    public List<MenuVo> listAll(Menu menu) {
        return menuMapper.listAll(menu);
    }

    @Override
    public boolean save(Menu menu) {
        boolean b = menuMapper.save(menu);
        if (b){
            Menu byName = this.getByName(menu.getName());
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setMenuId(byName.getId());
            roleMenuMapper.save(roleMenu);
        }
        return true;
    }

    @Override
    public void remove(Integer id) {
        menuMapper.remove(id);
        roleMenuMapper.remove(id);
    }

    @Override
    public void updateMenu(Menu menu) {
        menuMapper.update(menu);
    }

    @Override
    public Menu getByName(String name) {
        return menuMapper.getByName(name);
    }
}




